import {
  BaseScene
} from "./baseScene";

export class GameScene extends BaseScene {
  constructor() {
    super('GameScene')
    this.GameOver = false

    this.paddle_bottom = 50
    this.paddle_speed = 10

    this.ball_bottom = 80
    this.ball_speed = 400
    this.ball_isLunch = false
    this.balls = 3
    this.ballsText = null

    this.brick_width = 64
    this.brick_height = 32
    this.brick_firstx = 0
    this.brick_firstY = 50 + this.brick_height

  }
  // 生成砖块
  _createBrick() {
    this.bricks = this.physics.add.staticGroup()
    let bricks = ['brickBlue', 'brickGreen', 'brickPurple', 'brickRed', 'brickWhite', 'brickYellow']

    this.brick_maxRow = Math.floor(this.gameWidth / this.brick_width)
    this.brick_firstX = (this.gameWidth - this.brick_maxRow * this.brick_width + this.brick_width) / 2

    let X = this.brick_firstX,
      Y = this.brick_firstY

    for (let name of bricks) {
      for (let w = X; w + this.brick_height <= this.gameWidth; w += this.brick_width) {
        this.bricks.create(w, Y, name)
      }
      Y += this.brick_height
    }
    this.physics.add.collider(this.ball, this.bricks, this.killBrick, null, this)
  }
  // 生成挡板
  _createdPaddle() {
    this.paddle = this.physics.add.image(this.gameCenterX, this.gameHeight - this.paddle_bottom, 'paddle')
    this.paddle.setFriction(1, 1) // 设置摩擦力系数
    this.paddle.setImmovable() // 设置碰撞分离
    this.paddle.setCollideWorldBounds() // 画布边缘碰撞
  }
  // 生成弹球
  _createBall() {
    this.ball = this.physics.add.image(this.gameCenterX, this.gameHeight - this.ball_bottom, 'ball')
    this.ball.setBounce(1, 1) // 设置反弹值
    this.ball.setCollideWorldBounds() // 画布边缘碰撞

    this.physics.add.collider(this.ball, this.paddle, this._collisionPaddleFromBall, null, this) // 添加球与板的碰撞
  }
  // 计算反弹角度
  _collisionPaddleFromBall(ball, paddle) {
    // console.log(ball, paddle)
    // // 计算反弹角度
    // let positionDiff = Phaser.Math.Difference(paddle.x, ball.x);
    // let angle = ball.x > paddle.x ?
    //   95 + positionDiff / paddle.width * 2 * 55 :
    //   ball.x < paddle.x ?
    //   85 - positionDiff / paddle.width * 2 * 55 :
    //   Phaser.Math.Between(85, 95);
    //   let {
    //     veloctiyX,
    //     veloctiyY
    //   } = this._computedVeloctiy(angle)
    //   this.ball.setVelocity(veloctiyX, veloctiyY)

  }
  // 计算角度向量
  _computedVeloctiy(deg) {
    return {
      veloctiyX: Math.cos(deg) * this.ball_speed,
      veloctiyY: Math.sin(deg) * this.ball_speed
    }
  }
  // 发射一颗球
  _launch() {
    if (this.ball_isLunch) return
    this.ball_isLunch = true
    let deg = Phaser.Math.FloatBetween(Math.PI / 4, Math.PI * 3 / 4)
    let {
      veloctiyX,
      veloctiyY
    } = this._computedVeloctiy(deg)
    this.ball.setVelocity(veloctiyX, veloctiyY)
  }
  // 左右移动
  movePaddle(isRight) {
    let X = this.paddle.x + (isRight ? this.paddle_speed : -this.paddle_speed)
    let W2 = this.paddle.width / 2
    if (X - W2 <= 0 || X + W2 >= this.gameWidth) return
    this.paddle.setX(X)
    if (!this.ball_isLunch) this.ball.setX(X)
  }
  // 碰到砖块
  killBrick(ball, brick) {
    brick.disableBody(true, true) // 砖块消失
    if (this.bricks.countActive(true) == 0) this.gameVictory()

  }
  // 重新激活 全部砖块
  activeBricks() {
    this.bricks.children.iterate(function (child) {
      child.enableBody(true, child.x, child.y, true, true)
    })
  }
  // 小球掉出地图
  ballOut() {
    if (this.balls-- <= 0) return this.gameOver()

    this.ball_isLunch = false
    this.ballsText.setText(this.balls)
    this.ball.setPosition(this.paddle.x, this.gameHeight - this.ball_bottom)
    this.ball.setVelocity(0, 0)
  }
  //
  gameVictory() {
    setTimeout(() => {
      alert('you win!')
    }, 1000);
    this.reset()
  }
  //
  gameOver() {
    this.GameOver = true
    this.overImg = this.add.image(this.gameCenterX, this.gameCenterY, 'gameover')
    this.input.on('pointerdown', () => {
      this.input.off('pointerdown')
      this.reset()
    }, this)
  }
  reset() {
    if (this.overImg) this.overImg.destroy()
    this.GameOver = false
    this.balls = 3
    this.ballsText.setText(this.balls)

    this.activeBricks()
    this.paddle.setPosition(this.gameCenterX, this.gameHeight - this.paddle_bottom)
    this.ball.setPosition(this.paddle.x, this.gameHeight - this.ball_bottom)
    this.ball.setVelocity(0, 0)
    this.ball_isLunch = false
  }
  create() {
    // 设置世界碰撞系数 l r t b 底部取消碰撞
    this.physics.world.setBoundsCollision(true, true, true, false);

    this._createdPaddle()
    this._createBall()
    this._createBrick()

    this.ballsText = this.add.text(this.gameWidth - 40, 20, this.balls, {
      fontSize: 32,
      color: '#835995'
    })
    this.cursors = this.input.keyboard.createCursorKeys() // 创建键盘管理器
    this.input.keyboard.on('keydown-SPACE', this._launch.bind(this))
    this.input.on('pointerdown', this._launch.bind(this))

    this.input.on('pointermove', (e) => {
      if ((Math.abs(e.x - this.paddle.x)) < 10) return
      if (e.x < this.paddle.x) {
        this.movePaddle(false)
      } else {
        this.movePaddle(true)
      }
    })
    // this._launch()
  }
  update() {
    if (this.GameOver) return

    if (this.cursors.left.isDown) {
      this.movePaddle(false)
    } else if (this.cursors.right.isDown) {
      this.movePaddle(true)
    }
    if (this.ball.y > this.gameHeight) {
      this.ballOut()
    }
  }
}
